<template>
  <el-card class="box-cardAdmin">
    <div slot="header" class="clearfix">
      <span>{{ $t("admin.title") }}</span>
    </div>
    <el-form ref="textName" :model="textName">
      <el-form-item prop="text">
        <div class="smq">
          <el-input
            type="textarea"
            :rows="2"
            :placeholder="billboard.content"
            v-model="textName.text"
          >
          </el-input>
        </div>
      </el-form-item>
    </el-form>
    <div class="smq" align="right">
      <el-row>
        <el-button type="success" @click="submitText()">{{
          $t("admin.submit")
        }}</el-button>
        <el-button type="warning" @click="resetText()">{{
          $t("admin.delete")
        }}</el-button>
      </el-row>
    </div>
  </el-card>
</template>

<script type="text/javascript">
import { getBillboard } from "@/api/billboard";
import { createBillboard } from "@/api/billboard";
export default {
  name: "Admin",
  data() {
    return {
      textName: {
        text: "",
      },
      billboard: {
        content: "",
      },
    };
  },

  created() {
    this.fetchBillboard();
  },
  methods: {
    async fetchBillboard() {
      getBillboard().then((value) => {
        const { data } = value;
        this.billboard = data;
      });
    },
    async submitText() {
      if(this.textName.text == "") {
        this.$message({
                message: this.$t("admin.blank"),
                type: "warning",
                duration: 2000,
              });
      }else {
        createBillboard(this.textName).then((response) => {
        const { data } = response;
        this.billboard = data;
        this.textName.text = "";
        this.$message({
                message: this.$t("admin.success"),
                type: "success",
                duration: 2000,
              });
      });
      }
    },
    resetText() {
      this.$refs["textName"].resetFields();
    },
  },
};
</script>
